package com.rayo.accesscontrollercopy.app;

import android.content.Context;
import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.TextView;

import com.rayo.accesscontrollercopy.R;


/**
 * Created by ${Adam} on 16/3/8.
 */
public abstract class BaseFragment extends Fragment implements OnClickListener {
    protected View rootView;

    /**
     * 全局的Context {@link #mContext = this.getApplicationContext();}
     */
    protected Context mContext;

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        rootView = inflater.inflate(getLayoutId(), container, false);

        mContext = this.getActivity();
        preliminary();
        return rootView;
    }

    /**
     * 向用户展示信息前的准备工作在这个方法里处理
     */
    protected void preliminary() {
        // 初始化组件
        setupViews();

        // 初始化数据
        initialized();
    }

    /**
     * 获取全局的Context
     *
     * @return
     */
    public Context getContext() {
        return mContext;
    }

    /**
     * 布局文件ID
     *
     * @return
     */
    protected abstract int getLayoutId();

    /**
     * 初始化组件
     */
    protected abstract void setupViews();

    /**
     * 初始化数据
     */
    protected abstract void initialized();

    protected void replaceFragment(int containerViewId, Fragment fragment, String backStack) {
        FragmentTransaction ft = getFragmentManager().beginTransaction();
        ft.setCustomAnimations(R.anim.fade_in, R.anim.fade_out);
        ft.addToBackStack(backStack);
        ft.replace(containerViewId, fragment);
        ft.commit();
    }

    /**
     * 长时间显示Toast提示(来自String)
     *
     * @param message
     */
    protected void showSnackbar(View view, String message, OnClickListener snackbarClickListener) {
        count();
        Snackbar snackbar = Snackbar.make(view, message, Snackbar.LENGTH_LONG)
                .setAction(getString(R.string.okay), snackbarClickListener);
        setSnackbarMessageTextColor(snackbar, getResources().getColor(R.color.TextWhite));
        snackbar.show();
    }

    /**
     * 长时间显示Snackbar提示(来自res)
     *
     * @param resId
     */
    protected void showSnackbar(View view, int resId, OnClickListener snackbarClickListener) {
        count();
        Snackbar snackbar = Snackbar.make(view, getString(resId), Snackbar.LENGTH_LONG)
                .setAction(getString(R.string.okay), snackbarClickListener);
        setSnackbarMessageTextColor(snackbar, getResources().getColor(R.color.TextWhite));
        snackbar.show();
    }

    /**
     * 短暂显示Snackbar提示(来自res)
     *
     * @param resId
     */
    protected void showShortSnackbar(View view, int resId, OnClickListener snackbarClickListener) {
        count();
        Snackbar snackbar = Snackbar.make(view, getString(resId), Snackbar.LENGTH_SHORT)
                .setAction(getString(R.string.okay), snackbarClickListener);
        setSnackbarMessageTextColor(snackbar, getResources().getColor(R.color.TextWhite));
        snackbar.show();
    }

    /**
     * 短暂显示Snackbar提示(来自String)
     *
     * @param text
     */
    protected void showShortSnackbar(View view, String text, OnClickListener snackbarClickListener) {
        count();
        Snackbar snackbar = Snackbar.make(view, text, Snackbar.LENGTH_SHORT)
                .setAction(getString(R.string.okay), snackbarClickListener);
        setSnackbarMessageTextColor(snackbar, getResources().getColor(R.color.TextWhite));
        snackbar.show();
    }

    public static void setSnackbarMessageTextColor(Snackbar snackbar, int color) {
        View view = snackbar.getView();
        ((TextView) view.findViewById(R.id.snackbar_text)).setTextColor(color);
    }

    private int messageShowCount = 0;
    private int gcCount = 5;

    private void count() {
        messageShowCount++;
        if (messageShowCount >= gcCount) {
            System.gc();
            messageShowCount = 0;
        }
    }
}
